package com.green.domain;


import java.io.Serializable;

public class R<T> implements Serializable {

    /**
     * 状态码
     */
    private int code;

    /**
     * 数据
     */
    private T data;

    /**
     * 消息
     */
    private String message;

    /**
     * 描述
     */
    private String description;

    public R(int code, T data, String message, String description) {
        this.code = code;
        this.data = data;
        this.message = message;
        this.description = description;
    }

    public R(int code, T data, String message) {
        this(code, data, message, "");
    }

    public R(int code, T data) {
        this(code, data, null, null);
    }

    public R(ErrorCodeEnum errorCode) {
        this(errorCode.getCode(), null, errorCode.getMessage(), errorCode.getDescription());
    }

    public R() {
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }


    public static R success() {
        return new R(ErrorCodeEnum.SUCCESS);
    }

    public static <T> R<T> success(T data) {
        return new R<>(ErrorCodeEnum.SUCCESS.getCode(), data);
    }


    public static R error(ErrorCodeEnum errorCode) {
        return new R<>(errorCode);
    }

    public static R error(ErrorCodeEnum errorCode, String description) {
        return new R(errorCode.getCode(), errorCode.getMessage(), description);
    }

    public static R error(int code, String message) {
        return new R(code, null, message);
    }

    public static R error(String description) {
        return new R(ErrorCodeEnum.FAIL.getCode(), null, ErrorCodeEnum.FAIL.getMessage(), description);
    }

    public static R error(ErrorCodeEnum errorCode, String message, String description) {
        return new R(errorCode.getCode(), null, message, description);
    }

    public static R error(int code, String message, String description) {
        return new R(code, null, message, description);
    }


}
